<?php
/**
 * Handles the manipulation of the Eventsmanager database tables.
 *
 * YYYYY.
 * @package Eventsmanager
 * @author  Thato Selebogo
 */
include_once 'MySQLAdapter.php';
class database_tables_handler {
    
    protected  $_databaseAdapter;
    protected  $_databaseTable;

    /**
    * Class constructor.
    *
    * @access public
    * @param  MySQLAdapter   $databaseAdapter     The name of the database adapter used by this class
    * @return void
    */

    public function __construct()
    {
        $config = array("localhost","root","softwaredev3b","eventsmanager_v4");
////Get a singleton instance of the adapter
$adapter = MySQLAdapter::getInstance($config);
$adapter->connect();
        
        $this->_databaseAdapter = $adapter;
    }

    public function registerBusinessUser($userData)
    {
        $this->_databaseTable = 'buser';
        $id = $this->_databaseAdapter->insert($this->_databaseTable,$userData);
    }
    
        public function registerCustomerUser($userData)
    {
        $this->_databaseTable = 'cuser';
        $id = $this->_databaseAdapter->insert($this->_databaseTable,$userData);
    }


   public function insertLogoID($logoID)
   {
       $this->_databaseTable = 'event';
        $query = "insert into $this->_databaseTable (event_logo) value('$logoID')";
        $this->_databaseAdapter->query($query);
   }
    /**
    * Check if business username exists.
    * @param string $userName
    */

   public function BUsernameAvailability($userName)
   {
        $this->_databaseTable = 'buser';
        $query = "SELECT COUNT(*) AS count FROM $this->_databaseTable WHERE buser_username = '$userName'";
        $this->_databaseAdapter->query($query);
        $result = $this->_databaseAdapter->fetch();
        if(($result['count']) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
      


   }


       /**
    * Check if customer username exists.
    * @param string $userName
    */

   public function CUsernameAvailability($userName)
   {
        $this->_databaseTable = 'cuser';
        $query = "SELECT COUNT(*) AS count FROM $this->_databaseTable WHERE cuser_username = '$userName'";
        $this->_databaseAdapter->query($query);
        $result = $this->_databaseAdapter->fetch();
        if(($result['count']) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }



   }

    /**
    * Athenticate business user.
    * @param string $userName
    * @param string $password
    */

   public function authenticateBussinessUser($userName,$password)
   {
        $this->_databaseTable = 'buser';
        $query = "SELECT * FROM $this->_databaseTable WHERE buser_username = '$userName'AND buser_pword = '$password'";
        $this->_databaseAdapter->query($query);
        $result = $this->_databaseAdapter->fetch();

        if(($this->_databaseAdapter->countRows())>0)
        {
            return true;
        }
        else
        {
            return false;
        }
   }


       /**
    * Athenticate business user.
    * @param string $userName
    * @param string $password
    */

   public function authenticateCustomerUser($userName,$password)
   {
        $this->_databaseTable = 'cuser';
        $query = "SELECT * FROM $this->_databaseTable WHERE cuser_username = '$userName'AND cuser_pword = '$password'";
        $this->_databaseAdapter->query($query);
        $result = $this->_databaseAdapter->fetch();

        if(($this->_databaseAdapter->countRows())>0)
        {
            return true;
        }
        else
        {
            return false;
        }
   }

    /**
    * Retrive all business users.
    */

   public function getBusinessUserDetails($userName)
   {
        $this->_databaseTable = 'buser';
        $query = "SELECT * FROM $this->_databaseTable WHERE buser_username = '$userName'";
        $this->_databaseAdapter->query($query);
      // return $result = $this->_databaseAdapter->fetch();

        while ($row = $this->_databaseAdapter->fetch()) {
        extract($row);
        return $row;
       // return $buser_fname . ' - ' . $buser_lname . '<br/>';
        }

   }


      /**
    * Retrive all business users.
    */

   public function getCustomerUserDetails($userName)
   {
        $this->_databaseTable = 'cuser';
        $query = "SELECT * FROM $this->_databaseTable WHERE cuser_username = '$userName'";
        $this->_databaseAdapter->query($query);
      // return $result = $this->_databaseAdapter->fetch();

        while ($row = $this->_databaseAdapter->fetch()) {
        extract($row);
        return $row;
       // return $buser_fname . ' - ' . $buser_lname . '<br/>';
        }

   }

   public function getImageDetails($image_id)
   {
       $this->_databaseTable = 'images';
        $query = "SELECT * FROM $this->_databaseTable WHERE image_id = '$image_id'";
        $this->_databaseAdapter->query($query);
      // return $result = $this->_databaseAdapter->fetch();

        while ($row = $this->_databaseAdapter->fetch()) {
        extract($row);
        return $row;
       // return $buser_fname . ' - ' . $buser_lname . '<br/>';
        }
   }

   /**
    * Get business user id
    */

   public function getBuserId($userName, $password)
   {
        $this->_databaseTable = 'buser';
        $query = "SELECT buser_id FROM $this->_databaseTable WHERE buser_username = '$userName'AND buser_pword = '$password'";
        $this->_databaseAdapter->query($query);
        $result = $this->_databaseAdapter->fetch();
        return $result['buser_id'];

        }

     /**
    * Get business user id
    */

   public function getCuserId($userName, $password)
   {
        $this->_databaseTable = 'cuser';
        $query = "SELECT cuser_id FROM $this->_databaseTable WHERE cuser_username = '$userName'AND cuser_pword = '$password'";
        $this->_databaseAdapter->query($query);
        $result = $this->_databaseAdapter->fetch();
        return $result['cuser_id'];

        }
public function getLastInsertIDForImage()
{
        $this->_databaseTable = 'images';
        $query = "SELECT image_id FROM $this->_databaseTable order by image_id desc";
        $this->_databaseAdapter->query($query);
        $result = $this->_databaseAdapter->fetch();
        return $result['image_id'];
 
}
  public function insertEvent($buserId, $eventName, $eventAddress, $eventDescr,$eventType,$eventLat,$eventLong,$eventWebAddress='',$eventId='',$eventLogo='')
   {
        $this->_databaseTable = 'event';
        
            $data = array('event_id' => $eventId,
                      'buser_id' => $buserId,
                      'event_name' => $eventName,
                      'event_address' => $eventAddress,
                      'event_webaddress' => $eventWebAddress,
                      'event_logo' => $eventLogo,
                      'event_discr' => $eventDescr,
                      'event_type' => $eventType,
                      'event_loc_lat' => $eventLat,
                      'event_loc_long' => $eventLong,

        );
        $eventId = $this->_databaseAdapter->insert($this->_databaseTable,$data);
        return $eventId;

        }

     public function insertActivity($eventId, $openHr, $openMin, $closeHr,$closeMin,$actvityName,$activityDescr,$activityReqs='',$opTimeId='')
   {
        $this->_databaseTable = 'optime';

            $data = array('optime_id' => $opTimeId,
                      'optime_openhr' => $openHr,
                      'optime_openmin' => $openMin,
                      'optime_closehr' => $closeHr,
                      'optime_closemin' => $closeMin,
                      'optime_act_name' => $actvityName,
                      'optime_act_descr' => $activityDescr,
                      'optime_act_reqs' => $activityReqs,
                      'event_id' => $eventId,

        );
        $optimeId = $this->_databaseAdapter->insert($this->_databaseTable,$data);
        return $optimeId;

        }

    /**
    * Retrive all events.
    */
   public function getAllActivities($eventId)
   {
        $this->_databaseTable = 'optime';
        $this->_databaseAdapter->select($this->_databaseTable,"event_id=$eventId");
      //  $row = $this->_databaseAdapter->fetch();
             $resultArray = array();
         while ($row = $this->_databaseAdapter->fetch()) {
        extract($row);
      $resultArray[]= $row;
       // return $buser_fname . ' - ' . $buser_lname . '<br/>';
        }
  return $resultArray;
       // echo $row['event_name'];
    }

   public function getAllEvents($buser_id)
   {
        $this->_databaseTable = 'event';
        $query = "SELECT * FROM $this->_databaseTable WHERE buser_id = '$buser_id'";
        $this->_databaseAdapter->query($query);
     //   return $row = $this->_databaseAdapter->fetch();
        $resultArray = array();
        while ($row = $this->_databaseAdapter->fetch()) {
        extract($row);
      $resultArray[]= $row;
       // return $buser_fname . ' - ' . $buser_lname . '<br/>';
        }
  return $resultArray;
    }

       public function getVacinityEvents($userLat, $userLong)
   {
        $this->_databaseTable = 'event';
        $query = "SELECT * FROM $this->_databaseTable WHERE event_loc_lat <= '$userLat'+2 AND event_loc_lat >= '$userLat'-2
                AND event_loc_long <= '$userLong'+2 AND event_loc_long >= '$userLong'-2";
        $this->_databaseAdapter->query($query);
     //   return $row = $this->_databaseAdapter->fetch();
        $resultArray = array();
        while ($row = $this->_databaseAdapter->fetch()) {
        extract($row);
      $resultArray[]= $row;
       // return $buser_fname . ' - ' . $buser_lname . '<br/>';
        }
  return $resultArray;
    }
//   public function getBuserId($userName, $password)
//   {
//        $this->_databaseTable = 'buser';
//        $query = "SELECT buser_id FROM $this->_databaseTable WHERE buser_username = '$userName'AND buser_pword = '$password'";
//        $this->_databaseAdapter->query($query);
//        $result = $this->_databaseAdapter->fetch();
//        return $result['buser_id'];
//
//        }
    /**
    * Delete event by id.
    */

   public function deleteEvent($eventId)
   {
        $this->_databaseTable = 'event';
        return $this->_databaseAdapter->delete($this->_databaseTable,"event_id=$eventId");
    }

    /**
    * Retrieve event by id.
    */

   public function getEvent($eventId)
   {
        $this->_databaseTable = 'event';
        $this->_databaseAdapter->select($this->_databaseTable,"event_id=$eventId");
      //  $row = $this->_databaseAdapter->fetch();
             $resultArray = array();
         while ($row = $this->_databaseAdapter->fetch()) {
        extract($row);
      $resultArray[]= $row;
       // return $buser_fname . ' - ' . $buser_lname . '<br/>';
        }
  return $resultArray;
       // echo $row['event_name'];
    }

      /**
    * Update business user details.
    */

   public function updateBUserDetails($buserId,$firstName,$lastName,$userName,$passWord,$email)
   {
        $this->_databaseTable = 'buser';
            $data = array(
                'buser_fname' => $firstName,
                'buser_lname' => $lastName,
                'buser_username' => $userName,
                'buser_pword' => $passWord,
                'buser_email' => $email );

            return $this->_databaseAdapter->update($this->_databaseTable,$data,"buser_id=$buserId");
    }

 public function updateCUserDetails($cuserId,$firstName,$lastName,$userName,$passWord,$email)
   {
        $this->_databaseTable = 'cuser';
            $data = array(
                'cuser_fname' => $firstName,
                'cuser_lname' => $lastName,
                'cuser_username' => $userName,
                'cuser_pword' => $passWord,
                'cuser_email' => $email );

            return $this->_databaseAdapter->update($this->_databaseTable,$data,"cuser_id=$cuserId");
    }

        /**
    * Retrieve event by id.
    */

   public function getEventTypes()
   {
        $this->_databaseTable = 'event';
        $query = "SELECT event_type FROM $this->_databaseTable GROUP BY event_type";
        $this->_databaseAdapter->query($query);
       while($row = $this->_databaseAdapter->fetch())
       {   extract($row);
        //return $row;
        echo $row['event_type'];
       }

    }

}
?>
